﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Security.Cryptography;
using Power.Model;
using Power.DAL;

namespace Power
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Signup signup = new Signup();
            signup.ShowDialog();

            //string username = Usernabox.Text;
            //string userpass = Userpasswdbox.Password;
            //User op = new UserDal().GetByRootName(username);
            //if (op == null)
            //{
            //    MessageBox.Show("无此用户");
            //}
            //else
            //{

            //    string dbMd5 = op.Password; //数据库中存储的密码值
            //    string mymd5 = CommonHelper.GetMD5(userpass + CommonHelper.GetPasswordSalt());
            //    if (dbMd5 == mymd5)
            //    {
            //        MessageBox.Show("登录成功");
            //        DialogResult = true;
            //    }
            //    else
            //    {
            //        MessageBox.Show("用户名或者密码错误！");
            //    }
            //}







            
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            bool isOK = true;


            CheckTextboxNotEmpty(ref isOK, Usernabox);
            CheckPassboxNotEmpty(ref isOK, Userpasswdbox);
            if (!isOK)
            {
                MessageBox.Show("有未填写信息");
                return;
            }

            string username = Usernabox.Text;
            string userpass = Userpasswdbox.Password;
            User op = new UserDal().GetByUserName(username);
            if (op == null)
            {
                MessageBox.Show("无此用户");
            }
            else
            {

                string dbMd5 = op.Password; //数据库中存储的密码值
                string mymd5 = CommonHelper.GetMD5(userpass + CommonHelper.GetPasswordSalt());
                if (dbMd5 == mymd5)
                {
                    MessageBox.Show("登录成功");


                    ruser = username;

                    
                    DialogResult = true;
                }
                else
                {
                    MessageBox.Show("用户名或者密码错误！");
                }
            }









        }




        private void CheckTextboxNotEmpty   (ref bool isOK,
params TextBox[] textboxes)
        {
            foreach (TextBox txtBox in textboxes)
            {
                if (txtBox.Text.Length <= 0)
                {
                    isOK = false;
                    txtBox.Background = Brushes.Red;
                }
                else
                {
                    txtBox.Background = null;
                }
            }
        }

        private void CheckPassboxNotEmpty(ref bool isOK,
params PasswordBox[] passes)
        {
            foreach (PasswordBox passBox in passes)
            {
                if (passBox.Password.Length <= 0)
                {
                    isOK = false;
                    passBox.Background = Brushes.Red;
                }
                else
                {
                    passBox.Background = null;
                }
            }
        }


        public string ruser
        {

            get;
            set;
        }













        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }





















    }
}
